layui.config({
    base : "/static/js/modules/"
}).extend({
    "treetable" : "treetable/treetable",
    "common" : "common"
});
layui.use(['form', 'table', 'treetable', 'common'], function () {
    var form = layui.form;
    var $ = layui.jquery;
    var table = layui.table;
    var treetable = layui.treetable;
    var common = layui.common;

    // 渲染表格
    layer.load(2);
    treetable.render({
        treeColIndex: 1,
        treeSpid: 0,
        treeIdName: 'orgId',
        treePidName: 'parentId',
        elem: '#organizationList',
        url : '/organization/listData',
        page: false,
        id: 'organizationListTable',
        cols: [[
        	 {type: "checkbox", fixed:"left", width:50},
 			{field: 'orgCode', title: '组织单位编码', minWidth:120, align:"center"},
 			{field: 'orgName', title: '组织单位名称', minWidth:160, align:"center"},
// 			{field: 'parentId', title: '父级id', minWidth:100, align:"center"},
// 			{field: 'seq', title: '排序', minWidth:100, align:"center"}, 
 			{field: 'leader', title: '联系人', minWidth:100, align:"center"},
 			{field: 'phone', title: '手机号', minWidth:130, align:"center"},
 			{field: 'province', title: '所属区域', minWidth:200, align:"center",
 				templet: function (d) {
 					var cuntry = d.province === null ? '': d.province;
 					if(d.city !== null){
 						cuntry += '-'+d.city;
 					}
 					if(d.area !== null){
 						cuntry += '-'+d.area;
 					}
 					 return cuntry;
 				}
 			},
// 			{field: 'city', title: '市', minWidth:100, align:"center"},
// 			{field: 'area', title: '区', minWidth:100, align:"center"},
 			{field: 'adress', title: '地址', minWidth:200, align:"center"},
 			{
 				 field: 'statue', width: 100, align: 'center', templet: function (d) {
 	                    if (d.statue === 1) {
 	                        return '<span class="layui-badge layui-bg-green">启用</span>';
 	                    } else {
 	                        return '<span class="layui-badge layui-bg-cyan">禁用</span>';
 	                    }
 	                }, title: '状态'
 			},
 			{field: 'createUser', title: '创建人', minWidth:100, align:"center"},
 			{field: 'createTime', title: '创建时间', minWidth:100, align:"center"}
// 			{field: 'updateUser', title: '更新人', minWidth:100, align:"center"},
// 			{field: 'updateTime', title: '更新时间', minWidth:100, align:"center"}
        ]],
        done: function () {
            layer.closeAll('loading');
        }
    });

    //添加
    function addOrganization(edit){
        var title = "添加";
        if (edit){
            title = "编辑";
        }
        
        layui.layer.open({
            title : title,
            type : 2,
            area : ["600px","500px"],
            content : "info.html",
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                if(edit){
                    body.find("#orgId").val(edit.orgId);
                    body.find("#orgCode").val(edit.orgCode);
                    body.find("#orgName").val(edit.orgName);
                    body.find("#parentId").val(edit.parentId);
                    body.find("#seq").val(edit.seq);
                    body.find("#leader").val(edit.leader);
                    body.find("#phone").val(edit.phone);
                    body.find("#_province").val(edit.province);
                    body.find("#_city").val(edit.city);
                    body.find("#_area").val(edit.area);
                    body.find("#adress").val(edit.adress);
                    body.find("#statue").val(edit.statue);
                    body.find("#createUser").val(edit.createUser);
                    body.find("#createTime").val(edit.createTime);
                    
                    form.render();
                }
                setTimeout(function(){
                    layui.layer.tips('点击此处返回列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        })
    }

    $(".add_btn").click(function(){
        addOrganization();
    });

    $(".edit_btn").click(function(){
        var checkStatus = table.checkStatus('organizationListTable'),
            data = checkStatus.data;
        if(data.length > 0){
            addOrganization(data[0]);
        }else{
            layer.msg("请选择需要修改的记录");
        }
    });

    //批量删除
    $(".delAll_btn").click(function(){
        var checkStatus = table.checkStatus('organizationListTable'),
            data = checkStatus.data,
            idArr = [];
        if(data.length > 0) {
            for (var i in data) {
                idArr.push(data[i].id);
            }
            layer.confirm('确定删除选中的记录？', {icon: 3, title: '提示信息'}, function (index) {
                $.get("/organization/delBatch",{
                    idArr : idArr.toString()
                },function(data){
                    layer.close(index);
                    tableIns.reload();
                    if (data.data){
                        layer.msg("删除成功！");
                    } else {
                        layer.msg(data.msg);
                    }
                })
            })
        }else{
            layer.msg("请选择需要删除的记录");
        }
    })

});